package com.sm.honkon.ui.fragment.more

import android.graphics.Color
import android.graphics.drawable.BitmapDrawable
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.PopupWindow
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import au.com.safie.user.app.util.CacheUtil
import com.bigkoo.pickerview.builder.TimePickerBuilder
import com.bigkoo.pickerview.listener.OnTimeSelectListener
import com.bigkoo.pickerview.view.TimePickerView
import com.sm.honkon.R
import com.sm.honkon.app.base.BaseFragment
import com.sm.honkon.app.ext.getTimeFormat
import com.sm.honkon.app.handle.BottomHandle
import com.sm.honkon.data.bean.DeviceInfo
import com.sm.honkon.databinding.FrgSettingInfoBinding
import com.sm.honkon.ui.adapter.PopWindowAdapter
import com.sm.honkon.ui.model.SettingInfoViewModel
import kotlinx.android.synthetic.main.frg_setting_info.*
import me.hgj.jetpackmvvm.ext.nav
import java.util.*

class SettingInfoFragment : BaseFragment<SettingInfoViewModel, FrgSettingInfoBinding>() {
    override fun layoutId(): Int = R.layout.frg_setting_info

    var isChuchang = true

    val pickerView: TimePickerView by lazy {
        TimePickerBuilder(requireContext(),
            OnTimeSelectListener { date, _ ->
                if (isChuchang)
                    mViewModel.info.value!!.chuchangriqi.apply {
                        set(date.getTimeFormat("yyyy/MM/dd"))
                    } else
                    mViewModel.info.value!!.baoxiuqixian.apply {
                        set(date.getTimeFormat("yyyy/MM/dd"))
                    }
            })
            .setLayoutRes(R.layout.layout_time_picker) {
                it.findViewById<TextView>(R.id.btnCancel).setOnClickListener {
                    pickerView.dismiss()
                }
                it.findViewById<TextView>(R.id.btnSubmit).setOnClickListener {
                    pickerView.returnData()
                    pickerView.dismiss()
                }
            }.setDate(Calendar.getInstance())
            .setContentTextSize(10)
            .isDialog(false)
            .setOutSideCancelable(false)
            .setBgColor(Color.parseColor("#FFFFFF")).build()
    }

    val adapter by lazy {
        PopWindowAdapter().apply {
            setOnItemClickListener { adapter, view, position ->
                if (data == mViewModel.dianyafanweiList) {
                    mViewModel.info.value!!.dianyafanwei.apply {
                        set(data[position])
                    }
                } else {
                    mViewModel.info.value!!.zhuyaocanshu.apply {
                        set(data[position])
                    }
                }
                mPopWindow.dismiss()
            }
        }
    }

    val mPopWindow: PopupWindow by lazy {
        val contentView: View = LayoutInflater.from(requireContext())
            .inflate(R.layout.popwindow, null)
        PopupWindow(
            contentView,
            900, ViewGroup.LayoutParams.WRAP_CONTENT, true
        ).apply {
            this.contentView = contentView
            val recyclerView = contentView.findViewById<RecyclerView>(R.id.host_recycle)
            recyclerView.adapter = adapter
            //解决5.0以下版本点击外部不消失问题
            isOutsideTouchable = true
            setBackgroundDrawable(BitmapDrawable())
        }

    }


    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.model = mViewModel
        mDatabind.handle = BottomHandle(nav())
        tv_voltage.setOnClickListener {
            adapter.setNewInstance(mViewModel.dianyafanweiList)
            mPopWindow.showAsDropDown(it)
        }
        tv_parameter.setOnClickListener {
            adapter.setNewInstance(mViewModel.zhuyaocanshuList)
            mPopWindow.showAsDropDown(it)
        }
        tv_date_production.setOnClickListener {
            isChuchang = true
            pickerView.show()
        }
        tv_date_repair.setOnClickListener {
            isChuchang = false
            pickerView.show()
        }
        btn_save.setOnClickListener {
            CacheUtil.setDeviceInfo(mViewModel.info.value!!)
            nav().navigateUp()
        }
        btn_reset.setOnClickListener {
            mViewModel.info.postValue(DeviceInfo())
        }

    }

}